package com.xinou.daily_report.controller;

import com.alibaba.excel.EasyExcel;
import com.xinou.common.base.config.ApiResponse;
import com.xinou.common.base.exception.BusException;
import com.xinou.common.enumerate.HttpStatusEnum;
import com.xinou.daily_report.PO.WeekPlanPO;
import com.xinou.daily_report.PO.WeekPlanUpdatePO;
import com.xinou.daily_report.VO.WeekPlanExcelVO;
import com.xinou.daily_report.VO.WeekPlanInfoVO;
import com.xinou.daily_report.mapper.SysWeekPlanMapper;
import com.xinou.daily_report.service.SysWeekPlanService;
import com.xinou.service.file.service.FileUploadService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-06-11
 */
@RestController
@RequestMapping("/admin/weekPlan")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Api(tags = {"后台周计划"})
public class SysWeekPlanController {
    private final SysWeekPlanService sysWeekPlanService;

    private final SysWeekPlanMapper sysWeekPlanMapper;

    private final FileUploadService fileUploadService;
    public static final String UploadFileUrl="https://image.xiaokesi.com/";


    @PostMapping("/list")
    @ApiOperation("获取周报计划列表")
    @RequiresPermissions({"admin:weekPlan:list"})
    public ApiResponse<WeekPlanInfoVO> weekPlanList(@RequestBody WeekPlanPO weekPlanPO){
        System.out.println(weekPlanPO);

        return sysWeekPlanService.weekPlanList(weekPlanPO);
    }

    @PostMapping("/down")
    @ApiOperation("下载周计划")
    @RequiresPermissions({"admin:weekPlan:down"})
    public ApiResponse<String> weekPlanDown(@RequestBody WeekPlanPO po) throws IOException {
        List<WeekPlanExcelVO> vos = sysWeekPlanMapper.weekPlanDown(po);
        File tempFile = File.createTempFile(String.valueOf(System.nanoTime()),".xlsx");
        EasyExcel.write(tempFile, WeekPlanExcelVO.class).sheet("周计划").doWrite(vos);
        String s = fileUploadService.fileUpload(tempFile).orElseThrow(() -> new BusException(HttpStatusEnum.RE_CODE_PARAM_ERROR, "下载周计划失败"));
        return new ApiResponse<>(UploadFileUrl + s);
    }

    @PostMapping("/save")
    @ApiOperation("添加周报计划")
    @RequiresPermissions({"admin:weekPlan:save"})
    public ApiResponse<Void> weekPlanSave(@RequestBody WeekPlanUpdatePO weekPlanUpdatePO){
        System.out.println(weekPlanUpdatePO);
        return sysWeekPlanService.weekPlanSave(weekPlanUpdatePO);
    }

    @PostMapping("/update")
    @ApiOperation("修改周报计划")
    @RequiresPermissions({"admin:weekPlan:update"})
    public ApiResponse<Void> weekPlanUpdate(@RequestBody WeekPlanUpdatePO weekPlanUpdatePO){
        System.out.println(weekPlanUpdatePO);

        return sysWeekPlanService.weekPlanUpdate(weekPlanUpdatePO);
    }


    @PostMapping("/delete")
    @ApiOperation("删除周报计划")
    @RequiresPermissions({"admin:weekPlan:delete"})
    public ApiResponse<Void> weekPlanDelete(@RequestBody WeekPlanPO weekPlanPO){
        return sysWeekPlanService.weekPlanDelete(weekPlanPO);
    }

    @PostMapping("/getById")
    @ApiOperation("根据id查询周报计划")
    @RequiresPermissions({"admin:weekPlan:getById"})
    public ApiResponse<WeekPlanInfoVO> weekPlanGetById(@RequestBody WeekPlanPO weekPlanPO){
        return sysWeekPlanService.weekPlanGetById(weekPlanPO);
    }

    @PostMapping("/getByIdNoPaging")
    @ApiOperation("根据id查询周报结果不分页")
    @RequiresPermissions({"admin:weekPlan:getByIdNoPaging"})
    public ApiResponse<WeekPlanInfoVO> weekPlanGetByIdNoPaging(@RequestBody WeekPlanPO weekPlanPO){
        return sysWeekPlanService.weekPlanGetByIdNoPaging(weekPlanPO);
    }




}

